package com.sykmgj;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoTable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

public class GetSapConn {
    public static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL1";

    private static void createDataFile() {
        Properties proes = new Properties();
        File file = new File(JarPathUtil.getJarPath()+File.separator+"SapSykm.properties");
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            proes.load(inputStream);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }



        Properties properties = new Properties();
//        properties.setProperty("jco.client.ashost", "192.168.0.82");
//        properties.setProperty("jco.client.sysnr", "00");
//        properties.setProperty("jco.client.client", "800");
//        properties.setProperty("jco.client.user", "WEBSUSER");
//        properties.setProperty("jco.client.passwd", "p@ssw0rd");
//        properties.setProperty("jco.client.lang", "zh");
//        properties.setProperty("jco.client.codepage", "4110");

        properties.setProperty("jco.client.ashost", proes.getProperty("jco.client.ashost"));
        properties.setProperty("jco.client.sysnr", proes.getProperty("jco.client.sysnr"));
        properties.setProperty("jco.client.client", proes.getProperty("jco.client.client"));
        properties.setProperty("jco.client.user", proes.getProperty("jco.client.user"));
        properties.setProperty("jco.client.passwd", proes.getProperty("jco.client.passwd"));
        properties.setProperty("jco.client.lang", proes.getProperty("jco.client.lang"));
        properties.setProperty("jco.client.codepage", proes.getProperty("jco.client.codepage"));

        String name = ABAP_AS_POOLED;
        String suffix = "jcoDestination";
        File cfg = new File(String.valueOf(name) + "." + suffix);
        if (!cfg.exists())
            try {
                FileOutputStream fos = new FileOutputStream(cfg, false);
                properties.store(fos, "for tests only !");
                fos.close();
            } catch (Exception e) {
                throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
            }
    }

    public static JCoDestination getJcoConnection() {
        createDataFile();
        try {
            return JCoDestinationManager.getDestination(ABAP_AS_POOLED);
        } catch (JCoException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void startSyTask() {
        JCoDestination jCoDestination = null;
        JCoFunction function = null;
        try {
            jCoDestination = getJcoConnection();
            String interfacename = "ZFI_C1_SYKMFSE";
            function = jCoDestination.getRepository().getFunctionTemplate(interfacename).getFunction();
            if (function == null)
                throw new RuntimeException("get function not found in sap");
            function.execute(jCoDestination);
            List<String> tableDe = new ArrayList<>();
            StringBuffer colums = new StringBuffer();
            colums.append("TB_SYKMFSE:");
            colums.append("C1.FAGLLEXTZJBF:");
            colums.append("RACCT,");
            colums.append("RFAREA,");
            colums.append("RYEAR,");
            colums.append("RBUKRS,");
            colums.append("KOKRS,");
            colums.append("PRCTR,");
            colums.append("RCNTR,");
            colums.append("HSL01,");
            colums.append("HSL02,");
            colums.append("HSL03,");
            colums.append("HSL04,");
            colums.append("HSL05,");
            colums.append("HSL06,");
            colums.append("HSL07,");
            colums.append("HSL08,");
            colums.append("HSL09,");
            colums.append("HSL10,");
            colums.append("HSL11,");
            colums.append("HSL12,");
            colums.append("HSL13,");
            colums.append("HSL14,");
            colums.append("HSL15,");
            colums.append("HSL16");
            StringBuffer colums2 = new StringBuffer();
            colums2.append("TB_SKA1:");
            colums2.append("C1.SKA1:");
            colums2.append("KTOPL,");
            colums2.append("SAKNR,");
            colums2.append("TXT20,");
            colums2.append("TXT50");
            StringBuffer colums3 = new StringBuffer();
            colums3.append("TB_CEPC:");
            colums3.append("C1.CEPC:");
            colums3.append("KOKRS,");
            colums3.append("PRCTR,");
            colums3.append("KTEXT,");
            colums3.append("LTEXT");
            StringBuffer colums4 = new StringBuffer();
            colums4.append("TB_CSKS:");
            colums4.append("C1.CSKS:");
            colums4.append("KOKRS,");
            colums4.append("KOSTL,");
            colums4.append("KTEXT,");
            colums4.append("LTEXT");
            StringBuffer colums5 = new StringBuffer();
            colums5.append("TB_V_TFKB:");
            colums5.append("C1.V_TFKB:");
            colums5.append("FKBER,");
            colums5.append("FKBTX");
            tableDe.add(colums.toString());
            tableDe.add(colums2.toString());
            tableDe.add(colums3.toString());
            tableDe.add(colums4.toString());
            tableDe.add(colums5.toString());
            for (String tableDesc : tableDe) {
                String[] desc = tableDesc.split(":");
                String JCtable = desc[0];
                String table = desc[1];
                String colus = desc[2];
                JCoTable table2 = function.getTableParameterList().getTable(JCtable);
                String[] cols = colus.toString().split(",");
                StringTokenizer st = new StringTokenizer(colus.toString(), ",", false);
                String values = colus.toString();
                while (st.hasMoreTokens())
                    values = values.replaceFirst(st.nextToken(), "?");
                String sql = "INSERT INTO " + table + "(" + colus.toString() + ") VALUES(" + values + ")";
                JdbcConn jdbcConn = new JdbcConn();
                jdbcConn.executesql("truncate table " + table);
                jdbcConn.insert(table2, sql, cols, interfacename, table);
                System.out.println(table2.getNumRows());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
